package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-07-08 11:44:50 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/teachingMajor")
public class TeachingMajorController extends BaseController {
	@Autowired
	private TeachingMajorService teachingMajorService;
    @Autowired
    private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:teachingMajor:teachingMajor")
	String TeachingMajor(){
	    return "jzweb/teachingMajor/teachingMajor";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:teachingMajor:teachingMajor")
	public Result<Page<TeachingMajorDO>> list(TeachingMajorDO teachingMajorDTO){
        Wrapper<TeachingMajorDO> wrapper = new EntityWrapper<TeachingMajorDO>().orderBy("id", false);

        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            if(roleName.contains("讲师") && !roleName.contains("培训机构")){
                wrapper.eq("creater_id",this.getUserId());
            }
        }

        Page<TeachingMajorDO> page = teachingMajorService.selectPage(getPage(TeachingMajorDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			TeachingMajorDO obj = (TeachingMajorDO) page.getRecords().get(i);
            UserDO userDO = userService.selectById(obj.getCreaterId());
            if(userDO!=null && userDO.getUsername()!=null){
                obj.setCreaterName(userDO.getUsername());
            }else {
                obj.setCreaterName("");
            }
			list.add(obj);
		}
		if(list.size()>0){
            page.setRecords(list);
        }else {
            page.setRecords(null);
        }

        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:teachingMajor:add")
	String add(){
	    return "jzweb/teachingMajor/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:teachingMajor:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TeachingMajorDO teachingMajor = teachingMajorService.selectById(id);
		model.addAttribute("teachingMajor", teachingMajor);
	    return "jzweb/teachingMajor/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:teachingMajor:add")
	public Result<String> save( TeachingMajorDO teachingMajor){
	    teachingMajor.setCreaterDate(new Date());
	    teachingMajor.setCreaterId(this.getUserId());
		teachingMajorService.insert(teachingMajor);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:teachingMajor:edit")
	public Result<String>  update( TeachingMajorDO teachingMajor){
        teachingMajor.setUpdaterId(this.getUserId());
		boolean update = teachingMajorService.updateById(teachingMajor);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:teachingMajor:remove")
	public Result<String>  remove( Long id){
		teachingMajorService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:teachingMajor:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		teachingMajorService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
